package com.yuexinduo.education.ui;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.Window;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.android.arouter.launcher.ARouter;
import com.gyf.immersionbar.ImmersionBar;
import com.yuexinduo.baselibrary.util.LoginUtil;
import com.yuexinduo.education.R;
import com.yuexinduo.education.dialog.AgreementDialog;
import com.yuexinduo.education.utils.GuildUtil;

import java.util.Timer;
import java.util.TimerTask;

public class LaunchActivity extends AppCompatActivity implements AgreementDialog.OnItemClickListener {
    //定时器
    protected Timer timer = new Timer();
    //协议对话框
    private AgreementDialog dialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch);
        ImmersionBar.with(this)
                .transparentBar()
                .init();

        //判断是不是第一次登陆
        if (!GuildUtil.getIsNotFirst(this)) {
            showProductDialog();
        } else {
            startTimer();
        }
    }

    private void startTimer() {
        timer.schedule(tast, 1500);
    }

    TimerTask tast = new TimerTask() {
        @Override
        public void run() {
            if (!LoginUtil.isLogin(LaunchActivity.this)) {
//                startActivity(new Intent(LaunchActivity.this, LoginActivity.class));
                ARouter.getInstance()
                        .build("com.yuexinduo.loginmodule.LoginActivity")
//                        .withInt("i", i)
                        .navigation();
            } else {
                startActivity(new Intent(LaunchActivity.this, MainActivity.class));
            }
            finish();
        }
    };

    private void showProductDialog() {
        if (null != dialog && dialog.isShowing()) {
            return;
        }
        dialog = new AgreementDialog(this);
        dialog.setListener(this);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(false);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setLayout(600, ViewGroup.LayoutParams.WRAP_CONTENT);
        window.setBackgroundDrawable(new BitmapDrawable());
        dialog.show();
    }


    @Override
    public void OnItemClick(int action) {
        if (action == 0) {
            System.exit(0);
        } else if (action == 1) {

            if (!LoginUtil.isLogin(LaunchActivity.this)) {
//                startActivity(new Intent(LaunchActivity.this, LoginActivity.class));
                ARouter.getInstance()
                        .build("com.yuexinduo.loginmodule.LoginActivity")
//                        .withInt("i", i)
                        .navigation();
            } else {
                startActivity(new Intent(LaunchActivity.this, MainActivity.class));
            }
            finish();

        }
    }
}